package com.gin.danbooru.response.search

import com.gin.danbooru.enums.Rating
import org.jsoup.nodes.Element

/**
 * 作品预览框数据
 * @author BX-Gin
 * @from 2024/2/27 11:00
 */
class PostsPreview(article: Element) {
    /**
     * 作品id
     */
    var id: Long? = null

    /**
     * 缩略图地址
     */
    var thumbnailUrl: String? = null

    var height: Int? = null
    var width: Int? = null
    var type: String? = null
    var score: Int? = null
    var rating: Rating? = null
    var hasParent: Boolean? = null
    var hasChildren: Boolean? = null
    var tags: List<String>? = null
    var uploaderId: Long? = null

    /**
     * 动画的时长
     */
    var duration: String? = null


    init {
        // 解析 article标签中的数据

        id = article.attr("data-id").toLong()
        val img = article.getElementsByTag("img")[0]
        rating = Rating.find(article.attr("data-rating"))
        score = article.attr("data-score").toInt()
        tags = article.attr("data-tags").split(" ").toList()
        uploaderId = article.attr("data-uploader-id").toLong()

        val postDuration = article.getElementsByClass("post-duration")
        if (!postDuration.isEmpty()) {
            duration = postDuration[0].text().trim()
        }

        hasParent = article.hasClass("post-status-has-parent")
        hasChildren = article.hasClass("post-status-has-children")


        thumbnailUrl = img.attr("src")
        height = img.attr("height").toInt()
        width = img.attr("width").toInt()

        val source = article.getElementsByTag("source")[0]
        type = source.attr("type")
    }
}